<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>博客列表页</title>

    <link rel="stylesheet" href="css/common.css" />
    <link rel="stylesheet" href="css/list.css" />
  </head>
  <body>
    <div class="nav">
      <img src="pic/logo.jpg" alt="" />
      <span class="blog-title">我的博客系统</span>
      <div class="space"></div>
      <a class="nav-span" href="blog_list.html">主页</a>
      <a class="nav-span" href="blog_edit.html">写博客</a>
      <a class="nav-span" href="#" onclick="logout()">注销</a>
    </div>

    <div class="container">
      <div class="left">
        <div class="card profile-card">
          <div class="avatar-wrapper">
            <img class="avatar" src="pic/author.jpg" alt="" />
          </div>
          <h3 class="profile-name"></h3>
          <a class="github-link" href="#">Github地址</a>
          <div class="row stats-row">
            <span>文章</span>
            <span>分类</span>
          </div>
          <div class="row stats-row">
            <span>2</span>
            <span>1</span>
          </div>
        </div>
      </div>
      <div class="right">
        <!-- 使用JavaScript动态加载博客列表 -->
      </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/lib/marked.min.js"></script>
    <script src="js/common.js"></script>
    <script>
      //登录后左侧界面显示对应的用户信息
      $.ajax({
        url: "/user/getUserInfo?userId=" + localStorage.getItem("loginUserId"),
        type: "GET",
        success: function (result) {
          if (result.code === 200 && result.data != null) {
            console.log(result.data.githubUrl)
            $(".profile-name").text(result.data.userName);
            if(result.data.githubUrl) {
              $(".github-link")
                .attr("href", result.data.githubUrl)
            } else {
              $(".github-link")
                .attr("href", "#")
            }
            console.log(result.data.githubUrl)
            //$(".github-link").attr("href", result.data.githubUrl);
          } else {
            alert("获取用户信息失败，请稍后再试。");
          }
        },
      });

      //使用ajax获取博客列表，服务器返回JSON数据
      $.ajax({
        url: "/blog/getList",
        type: "GET",
        success: function (result) {
          //返回数据格式是{"data":[{"id":,"title":"","content":"","userId":,"updateTime":""},{}],"code":200,"errMsg":null}
          if (result.code == 200 && result.data && result.data.length > 0) {
            var blogs = result.data;
            var blogListHtml = "";
            for (var i = 0; i < blogs.length; i++) {
              var blog = blogs[i];
              // 使用marked将markdown转换为HTML，并提取纯文本
              var contentHtml = marked.parse(blog.content);
              var contentText = $("<div>").html(contentHtml).text();

              blogListHtml += '<div class="blog">';
              blogListHtml += '<div class="title">' + blog.title + "</div>";
              blogListHtml += '<div class="date">' + blog.updateTime + "</div>";
              blogListHtml +=
                '<div class="desc">' +
                (contentText.length > 10
                  ? contentText.substring(0, 10) + "..."
                  : contentText) +
                "</div>";
              blogListHtml +=
                '<a class="detail" href="blog_detail.html?blogId=' +
                blog.id +
                '">查看全文&gt;&gt;</a>';
              blogListHtml += "</div>";
            }
            $(".right").html(blogListHtml);
          } else {
            $(".right").html("<p>暂无博客内容</p>");
          }
        },
      });
    </script>
  </body>
</html>
